package o;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build.VERSION;
import android.util.Log;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

final class ls implements Closeable {
    private final FileLock ʻ;
    private final long ˊ;
    private final RandomAccessFile ˋ;
    private final File ˎ;
    private final File ˏ;
    private final FileChannel ॱ;

    static class c extends File {
        public long ˎ = -1;

        public c(File file, String str) {
            super(file, str);
        }
    }

    ls(File file, File file2) throws IOException {
        RuntimeException e;
        IOException e2;
        Log.i("MultiDex", "MultiDexExtractor(" + file.getPath() + ", " + file2.getPath() + ")");
        this.ˎ = file;
        this.ˏ = file2;
        this.ˊ = ॱ(file);
        File file3 = new File(file2, "MultiDex.lock");
        this.ˋ = new RandomAccessFile(file3, "rw");
        try {
            this.ॱ = this.ˋ.getChannel();
            try {
                Log.i("MultiDex", "Blocking on lock " + file3.getPath());
                this.ʻ = this.ॱ.lock();
                Log.i("MultiDex", file3.getPath() + " locked");
            } catch (IOException e3) {
                e = e3;
                ˋ(this.ॱ);
                throw e;
            } catch (RuntimeException e4) {
                e = e4;
                ˋ(this.ॱ);
                throw e;
            } catch (Error e5) {
                e = e5;
                ˋ(this.ॱ);
                throw e;
            }
        } catch (IOException e6) {
            e2 = e6;
            ˋ(this.ˋ);
            throw e2;
        } catch (RuntimeException e7) {
            e2 = e7;
            ˋ(this.ˋ);
            throw e2;
        } catch (Error e8) {
            e2 = e8;
            ˋ(this.ˋ);
            throw e2;
        }
    }

    List<? extends File> ॱ(Context context, String str, boolean z) throws IOException {
        Log.i("MultiDex", "MultiDexExtractor.load(" + this.ˎ.getPath() + ", " + z + ", " + str + ")");
        if (this.ʻ.isValid()) {
            List<? extends File> ˎ;
            if (z || ˋ(context, this.ˎ, this.ˊ, str)) {
                if (z) {
                    Log.i("MultiDex", "Forced extraction must be performed.");
                } else {
                    Log.i("MultiDex", "Detected that extraction must be performed.");
                }
                ˎ = ˎ();
                ˋ(context, str, ˋ(this.ˎ), this.ˊ, ˎ);
            } else {
                try {
                    ˎ = ˋ(context, str);
                } catch (Throwable e) {
                    Log.w("MultiDex", "Failed to reload existing extracted secondary dex files, falling back to fresh extraction", e);
                    ˎ = ˎ();
                    ˋ(context, str, ˋ(this.ˎ), this.ˊ, ˎ);
                }
            }
            Log.i("MultiDex", "load found " + ˎ.size() + " secondary dex files");
            return ˎ;
        }
        throw new IllegalStateException("MultiDexExtractor was closed");
    }

    public void close() throws IOException {
        this.ʻ.release();
        this.ॱ.close();
        this.ˋ.close();
    }

    private List<c> ˋ(Context context, String str) throws IOException {
        Log.i("MultiDex", "loading existing secondary dex files");
        String str2 = this.ˎ.getName() + ".classes";
        SharedPreferences ˏ = ˏ(context);
        int i = ˏ.getInt(str + "dex.number", 1);
        List<c> arrayList = new ArrayList(i - 1);
        int i2 = 2;
        while (i2 <= i) {
            File cVar = new c(this.ˏ, str2 + i2 + ".zip");
            if (cVar.isFile()) {
                cVar.ˎ = ॱ(cVar);
                long j = ˏ.getLong(str + "dex.crc." + i2, -1);
                long j2 = ˏ.getLong(str + "dex.time." + i2, -1);
                long lastModified = cVar.lastModified();
                if (j2 == lastModified && j == cVar.ˎ) {
                    arrayList.add(cVar);
                    i2++;
                } else {
                    throw new IOException("Invalid extracted dex: " + cVar + " (key \"" + str + "\"), expected modification time: " + j2 + ", modification time: " + lastModified + ", expected crc: " + j + ", file crc: " + cVar.ˎ);
                }
            }
            throw new IOException("Missing extracted secondary dex file '" + cVar.getPath() + "'");
        }
        return arrayList;
    }

    private static boolean ˋ(Context context, File file, long j, String str) {
        SharedPreferences ˏ = ˏ(context);
        return (ˏ.getLong(new StringBuilder().append(str).append("timestamp").toString(), -1) == ˋ(file) && ˏ.getLong(str + "crc", -1) == j) ? false : true;
    }

    private static long ˋ(File file) {
        long lastModified = file.lastModified();
        if (lastModified == -1) {
            return lastModified - 1;
        }
        return lastModified;
    }

    private static long ॱ(File file) throws IOException {
        long ˏ = mb.ˏ(file);
        if (ˏ == -1) {
            return ˏ - 1;
        }
        return ˏ;
    }

    private List<c> ˎ() throws IOException {
        String str = this.ˎ.getName() + ".classes";
        ॱ();
        List<c> arrayList = new ArrayList();
        ZipFile zipFile = new ZipFile(this.ˎ);
        ZipEntry entry = zipFile.getEntry("classes" + 2 + ".dex");
        int i = 2;
        while (entry != null) {
            File cVar = new c(this.ˏ, str + i + ".zip");
            arrayList.add(cVar);
            Log.i("MultiDex", "Extraction is needed for file " + cVar);
            Object obj = null;
            int i2 = 0;
            while (i2 < 3 && obj == null) {
                int i3 = i2 + 1;
                ˎ(zipFile, entry, cVar, str);
                try {
                    cVar.ˎ = ॱ(cVar);
                    obj = 1;
                } catch (Throwable e) {
                    Log.w("MultiDex", "Failed to read crc from " + cVar.getAbsolutePath(), e);
                    obj = null;
                } catch (Throwable th) {
                    try {
                        zipFile.close();
                    } catch (Throwable e2) {
                        Log.w("MultiDex", "Failed to close resource", e2);
                    }
                }
                Log.i("MultiDex", "Extraction " + (obj != null ? "succeeded" : "failed") + " '" + cVar.getAbsolutePath() + "': length " + cVar.length() + " - crc: " + cVar.ˎ);
                if (obj == null) {
                    cVar.delete();
                    if (cVar.exists()) {
                        Log.w("MultiDex", "Failed to delete corrupted secondary dex '" + cVar.getPath() + "'");
                        i2 = i3;
                    }
                }
                i2 = i3;
            }
            if (obj == null) {
                throw new IOException("Could not create zip file " + cVar.getAbsolutePath() + " for secondary dex (" + i + ")");
            }
            int i4 = i + 1;
            entry = zipFile.getEntry("classes" + i4 + ".dex");
            i = i4;
        }
        try {
            zipFile.close();
        } catch (Throwable e3) {
            Log.w("MultiDex", "Failed to close resource", e3);
        }
        return arrayList;
    }

    private static void ˋ(Context context, String str, long j, long j2, List<c> list) {
        Editor edit = ˏ(context).edit();
        edit.putLong(str + "timestamp", j);
        edit.putLong(str + "crc", j2);
        edit.putInt(str + "dex.number", list.size() + 1);
        int i = 2;
        for (c cVar : list) {
            edit.putLong(str + "dex.crc." + i, cVar.ˎ);
            edit.putLong(str + "dex.time." + i, cVar.lastModified());
            i++;
        }
        edit.commit();
    }

    private static SharedPreferences ˏ(Context context) {
        return context.getSharedPreferences("multidex.version", VERSION.SDK_INT < 11 ? 0 : 4);
    }

    private void ॱ() {
        File[] listFiles = this.ˏ.listFiles(new FileFilter(this) {
            final /* synthetic */ ls ˎ;

            {
                this.ˎ = r1;
            }

            public boolean accept(File file) {
                return !file.getName().equals("MultiDex.lock");
            }
        });
        if (listFiles == null) {
            Log.w("MultiDex", "Failed to list secondary dex dir content (" + this.ˏ.getPath() + ").");
            return;
        }
        for (File file : listFiles) {
            Log.i("MultiDex", "Trying to delete old file " + file.getPath() + " of size " + file.length());
            if (file.delete()) {
                Log.i("MultiDex", "Deleted old file " + file.getPath());
            } else {
                Log.w("MultiDex", "Failed to delete old file " + file.getPath());
            }
        }
    }

    private static void ˎ(ZipFile zipFile, ZipEntry zipEntry, File file, String str) throws IOException, FileNotFoundException {
        Closeable inputStream = zipFile.getInputStream(zipEntry);
        File createTempFile = File.createTempFile("tmp-" + str, ".zip", file.getParentFile());
        Log.i("MultiDex", "Extracting " + createTempFile.getPath());
        ZipOutputStream zipOutputStream;
        try {
            zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(createTempFile)));
            ZipEntry zipEntry2 = new ZipEntry("classes.dex");
            zipEntry2.setTime(zipEntry.getTime());
            zipOutputStream.putNextEntry(zipEntry2);
            byte[] bArr = new byte[16384];
            for (int read = inputStream.read(bArr); read != -1; read = inputStream.read(bArr)) {
                zipOutputStream.write(bArr, 0, read);
            }
            zipOutputStream.closeEntry();
            zipOutputStream.close();
            if (createTempFile.setReadOnly()) {
                Log.i("MultiDex", "Renaming to " + file.getPath());
                if (createTempFile.renameTo(file)) {
                    ˋ(inputStream);
                    createTempFile.delete();
                    return;
                }
                throw new IOException("Failed to rename \"" + createTempFile.getAbsolutePath() + "\" to \"" + file.getAbsolutePath() + "\"");
            }
            throw new IOException("Failed to mark readonly \"" + createTempFile.getAbsolutePath() + "\" (tmp of \"" + file.getAbsolutePath() + "\")");
        } catch (Throwable th) {
            ˋ(inputStream);
            createTempFile.delete();
        }
    }

    private static void ˋ(Closeable closeable) {
        try {
            closeable.close();
        } catch (Throwable e) {
            Log.w("MultiDex", "Failed to close resource", e);
        }
    }
}
